-- Decompiled with the Synapse X Luau decompiler.

local ServerOHOH = _G.ServerOHOH;
local event_list = ServerOHOH.event_list;
event_list.server_player_character = true;
local function_list = ServerOHOH.function_list;
local event_manager = ServerOHOH.event_manager();
local InstanceManager = ServerOHOH.load.InstanceManager();
local M = ServerOHOH.class.new(script, ServerOHOH.files.PlayerObject());
function M.ctor(p1, player)
	M.super.ctor(p1, player);
end;
function M.dtor(p3)
	M.super.dtor(p3);
end;
function M.Fire(p4, p5, ...)
	event_manager:Fire(p4, p5, ...);
end;
local function_manager = ServerOHOH.function_manager();
function M.Invoke(self, p7, ...)
	function_manager:Invoke(self, p7, ...);
end;

--登录，获取玩家数据
function M.Login(self)
	--print("PPA--->Login")
	--获取玩家数据
	self.save_data = self:AddComponent(ServerOHOH.load.Server_PlayerDataComponent());
	-- print("获取到用户数据");
	if self.OnLogin then
		local s, d = xpcall(self.OnLogin, debug.traceback, self);
		if not s then
			warn(d);
		end;
	end;
	return self.save_data.__save_data.__real;
end;

local function characterEnter(player, characterInst)
	-- print("PPA--->characterEnter-0",player,characterInst)

	local characterClass=player:GetServerCharacter()
	local character = ServerOHOH.load[characterClass]().new(characterInst, player);
	player.character = character;
	--print("PPA--->characterEnter-1",player,characterInst,character)

	player:CharacterIn(character);
	--print("PPA--->characterEnter-2",player,characterInst)

	-- 通知客户端创建角色
	event_manager:Fire(player, event_list.server_player_character, characterInst, player:GetClientCharacter());

end;

local function characterOut(server_playerObject, characterInst)
	server_playerObject.character = nil;
	local character = InstanceManager:FindObject(characterInst);
	-- print("PPA--->characterOut-server_playerObject",character)
	if character then
		-- print("PPA--CharacterOut-1->")
		server_playerObject:CharacterOut(character);
		-- print("PPA--CharacterOut-_2->")

		character:dtor();
		-- print("PPA--CharacterOut-_3->")

	end;
end;

--登录完成
function M.LoginOver(self)
	if self.OnLoginOver then
		local v8, v9 = xpcall(self.OnLoginOver, debug.traceback, self);
		if not v8 then
			warn(v9);
		end;
	end;
	
	--character 生成监听 确保character生成，然后调用 characterEnter：：self，character
	self.main_inst.CharacterAdded:Connect(function(character)
		
		--确保character生成，然后调用 characterEnter
		self.__character_creating = true;
		if not character.Parent then
			local u10 = nil;
			u10 = character.Changed:Connect(function(p15)
				if p15 == "Parent" and character.Parent then
					u10:Disconnect();
					local v10, v11 = xpcall(characterEnter, debug.traceback, self, character);
					if not v10 then
						warn(v11);
					end;
					self.__character_creating = nil;
				end;
			end);
			return;
		end;
		local v12, v13 = xpcall(characterEnter, debug.traceback, self, character);
		if not v12 then
			warn(v13);
		end;
		self.__character_creating = nil;
		
		
	end);
	
	--character移除监听 character删除，然后调用 characterOut  ：self，character

	self.main_inst.CharacterRemoving:Connect(function(p16)
		--print("PPA--->main_inst.CharacterRemoving",p16)

		local v14, v15 = xpcall(characterOut, debug.traceback, self, p16);
		if not v14 then
			warn(v15);
		end;
	end);
	
	--如果character 已经存在，调用characterEnter ：self，character
	local l__Character__16 = self.main_inst.Character;
	if l__Character__16 and not self.__character_creating and not self.character then
		self.__character_creating = true;
		local v17, v18 = xpcall(characterEnter, debug.traceback, self, l__Character__16);
		if not v17 then
			warn(v18);
		end;
		self.__character_creating = nil;
	end;
end;

--创建角色
function M.CreateCharacter(self)
	--print("PPA--->CreateCharacter-1")

	self.main_inst:LoadCharacter();
	--while not self.character do
	--	wait();	
	--end;
	--return self.character;
end;

--删除角色
function M.RemoveCharacter(self)
	
	local character=self.character
	self.character = nil
	--print("PPA--->RemoveCharacter-0",character)
	self:CharacterOut(character);
	--print("PPA--->RemoveCharacter-1",character)
	character:dtor();
	--print("PPA--->RemoveCharacter-character:dtor")

end;

function M.SetServerCharacter(p19, p20)
	assert(p20, "param class name is nil");
	p19.attribute.server_character_name = p20;
end;
function M.SetClientCharacter(p21, p22)
	assert(p22, "param class name is nil");
	p21.attribute.client_character_class = p22;
end;
function M.GetServerCharacter(self)
	return self.attribute.server_character_class or "Server_PlayerCharacter";
end;
function M.GetClientCharacter(self)
	return self.attribute.client_character_class or "Client_PlayerCharacter";
end;

--角色被创建
function M.CharacterIn(self, p26)
	--print("PPA--->CharacterIn",p26)

	if self.OnCharacterIn then
		self:AddProcess(self.OnCharacterIn, p26);
	end;
	
end;
--角色被移除
function M.CharacterOut(self, p28)
	--print("PPA--->CharacterOut",p28)

	if self.OnCharacterOut then
		self:OnCharacterOut(p28)
		--self:AddProcess(self.OnCharacterOut, p28);
	end;
end;

--客户端调用
function_manager:SetHandle(function_list.player_login, M.Login);
function_manager:SetHandle(function_list.player_login_over, M.LoginOver);
return M;
